package dvx.mp.ivisual.desktop.ui.util;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class FileExtension extends FileFilter {
	public final static String JPEG = "jpeg";
	public final static String JPG = "jpg";
	public final static String GIF = "gif";
	public final static String TIFF = "tiff";
	public final static String TIF = "tif";
	public final static String PNG = "png";
	
	public final static String EXE = "exe";
	
	public final static String XML = "xml";
	
	
	private String fileFilter;
	public FileExtension(String fileFilter)	{
		this.fileFilter = fileFilter;
	}
	
	/**
	 * Returns the extension for a file
	 * @param f File wich we want to know its extension
	 * @return The file extension
	 */
	public static String getExtension(File f) {
		String ext = null;
		String s = f.getName();
		int i = s.lastIndexOf('.');

		if (i > 0 &&  i < s.length() - 1) {
			ext = s.substring(i+1).toLowerCase();
		}
		return ext;
	}

	@Override
	public boolean accept(File arg) {
		if (arg.isDirectory()) {
			return true;
		}
		
		boolean permitirExtension = false;
		String extension = FileExtension.getExtension(arg);
		if (extension != null) {
			if (extension.equals(fileFilter)) {
				permitirExtension = true;
			}
		}
		return permitirExtension;
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}
}
